package org.wk.account.controller;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.joda.time.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.wk.account.domain.ling.SubAccountLingExchangeCNY;
import org.wk.account.service.SubAccountLingService;
import org.wk.account.service.SubAccountZuanService;
import org.wk.account.view.LingExchangeCNYVo;
import org.wk.core.common.tool.ExcelUtils;
import org.wk.core.common.view.PageVo;
import org.wk.core.common.view.ResponseVo;
import org.wk.logger.common.annotation.SysLog;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @ClassName LingExchangeCNYController
 * @Description     提现管理
 * @Author ZhaoPing'an
 * @Date 2018/6/28 17:14
 */
@RestController
@RequestMapping("/exchangeCNY")
public class LingExchangeCNYController {

    @Autowired
    private SubAccountLingService lingService;

    /**
     * @Description 鲸灵提现管理列表
     * @param
     * @author ZhaoPing'an
     * @Date 2018/7/2 16:17
     */
    @SysLog("鲸灵提现管理列表")
    @RequestMapping(value="/ling/list")
    public ResponseVo list(@RequestParam Map<String, Object> params){
        PageVo page = lingService.findExchangeCNYListPage(params);
        return ResponseVo.success().addAttribute("page", page);
    }

    /**
     * @Description 已打款
     * @param
     * @author ZhaoPing'an
     * @Date 2018/7/2 16:17
     */
    @SysLog("鲸灵提现已打款操作")
    @RequestMapping(value="/ling/successExchangeCNY")
    public ResponseVo successExchangeCNY(@RequestBody SubAccountLingExchangeCNY lingExchangeCNY){
        ResponseVo result = lingService.successExchangeCNY(lingExchangeCNY.getId());
        return result;
    }

    @SysLog("鲸灵提现拒绝操作")
    @RequestMapping(value="/ling/refuseExchangeCNY")
    public ResponseVo refuseExchangeCNY(@RequestBody LingExchangeCNYVo lingExchangeCNYVo){
        ResponseVo result = lingService.refuseExchangeCNY(lingExchangeCNYVo);
        return result;
    }

    /**
     * @Description 鲸灵提现记录
     * @param
     * @author ZhaoPing'an
     * @Date 2018/7/4 14:44
     */
    @SysLog("鲸灵提现记录")
    @RequestMapping(value="/ling/exchangeCnySeq")
    public ResponseVo exchangeCnySeq(@RequestParam Map<String, Object> params){
        PageVo page = lingService.findExchangeCNYSeq(params);
        return ResponseVo.success().addAttribute("page", page);
    }

    /*
     * @description 导出提现记录到Excel
     * @param
     * @author ZhaoPing'an
     * @date 2018/7/24 10:54
     */
    @SysLog("导出提现记录")
    @RequestMapping(value="/ling/exportSeqs")
    public void exportSeqs(@RequestParam Map<String, Object> params,HttpServletResponse response){

        try {
            String[] rowsName = new String[]{"序号","姓名","手机号","收款类型","金额","收款账户","提现状态","拒绝原因","申请时间"};
            // 获取填充数据
            List<Object[]> datas = lingService.exportSeqs(params,rowsName.length);
            String fileName = "提现记录"+ new LocalDate().toString("yyyyMMdd") +".xls";
            // 生成Excel
            HSSFWorkbook workbook = ExcelUtils.getHSSFWorkbook("提现记录",rowsName,datas);
            OutputStream os = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            //response.setHeader("Content-disposition","attachment;filename="+fileName);
            response.setHeader("Content-disposition","attachment;filename*=UTF-8''" + URLEncoder.encode(fileName,"UTF-8"));
            workbook.write(os);
            os.flush();
            os.close();
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
